﻿/*************************   Auto Generated Repository Code For User table     Generated @ 4/15/2013 4:43:12 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class UserRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an User by its UserID (Primary Key)
        /// </summary>
        /// <param name="UserID">The ID of the <see cref="User"/> to retrieve</param>
        /// <returns>A <see cref="User"/> object</returns>
        public BusinessObjects.User Select(int UserID)
        {
            User retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Users where x.UserID == UserID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        
        /// <summary>
        /// Return a list of User objects
        /// </summary>
        /// <returns>A List of type <see cref="User"/></returns>
        public IList<BusinessObjects.User> SelectAll()
        {
            List<BusinessObjects.User> retObject = new List<BusinessObjects.User>();

            using (dB = new PracticeSimpleDataContext())
            {
                var UserList = (from x in dB.Users select x).ToList();

                foreach (User obj in UserList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a User Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public void Save(BusinessObjects.User businessObject)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                dB.Users.InsertOnSubmit(ConvertToDataObject(businessObject));
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Accepts the Primary Key of a User, finds it in the database and deletes it
        /// </summary>
        /// <param name="UserID">The ID of the <see cref="User"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int UserID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.User obj = (from x in dB.Users where x.UserID == UserID select x).SingleOrDefault();
                dB.Users.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a User data object to a User business object
        /// </summary>
        /// <returns>A business object of type <see cref="User"/></returns>
        public BusinessObjects.User ConvertToBusinessObject(DataLayer.User dataObject)
        {
            BusinessObjects.User businessObject = new BusinessObjects.User();

            businessObject.UserID = dataObject.UserID;
            businessObject.FacilityID = dataObject.FacilityID;
            businessObject.Fname = dataObject.Fname;
            businessObject.Lname = dataObject.Lname;
            businessObject.Username = dataObject.Username;
            businessObject.Password = dataObject.Password;


            return businessObject;

        }

        /// <summary>
        /// Converts a User business object to a User data object
        /// </summary>
        /// <returns>A data object of type <see cref="User"/></returns>
        public DataLayer.User ConvertToDataObject(BusinessObjects.User businessObject)
        {
            DataLayer.User dataObject = new DataLayer.User();

            dataObject.UserID = businessObject.UserID;
            dataObject.FacilityID = businessObject.FacilityID;
            dataObject.Fname = businessObject.Fname;
            dataObject.Lname = businessObject.Lname;
            dataObject.Username = businessObject.Username;
            dataObject.Password = businessObject.Password;


            return dataObject;
        }
    }
}


